package com.yf.thumbbackend.service.impl;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.util.ObjUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.yf.thumbbackend.constant.ThumbConstant;
import com.yf.thumbbackend.entity.Blog;
import com.yf.thumbbackend.entity.User;
import com.yf.thumbbackend.entity.vo.BlogVO;
import com.yf.thumbbackend.mapper.BlogMapper;
import com.yf.thumbbackend.service.BlogService;
import com.yf.thumbbackend.service.ThumbService;
import com.yf.thumbbackend.service.UserService;
import jakarta.annotation.Resource;
import jakarta.servlet.http.HttpServletRequest;
import lombok.RequiredArgsConstructor;
import org.springframework.context.annotation.Lazy;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

/**
* @author echo-sp
* @description 针对表【blog】的数据库操作Service实现
* @createDate 2025-04-18 20:27:12
*/
@Service
@RequiredArgsConstructor
public class BlogServiceImpl extends ServiceImpl<BlogMapper, Blog>
    implements BlogService{

    @Resource
  private   UserService userService;
    @Resource
    @Lazy //防止循环依赖
    private ThumbService thumbService;
    private RedisTemplate<String,Object> redisTemplate;
    @Override
    public BlogVO getBlogById(Long id, HttpServletRequest request) {
        Blog blog = this.getById(id);
        //获取当前用户信息
        User loginUser = userService.getLoginUser(request);
        return this.getBlogVoById(blog,loginUser);

    }

    @Override
    public List<BlogVO> getBlogVoList(List<Blog> list, HttpServletRequest request) {

        Map<Long, Boolean> thumbMap =new HashMap<>();
        // 获取当前登录用户
        User loginUser = userService.getLoginUser(request);

        if (ObjUtil.isNotEmpty(loginUser)) {
            List<Object> blogIdList = list.stream().map(blog -> blog.getId().toString()).collect(Collectors.toList());
            // 获取点赞
            List<Object> thumbList = redisTemplate.opsForHash().multiGet(ThumbConstant.USER_THUMB_KEY_PREFIX + loginUser.getId(), blogIdList);
            for (int i = 0; i < thumbList.size(); i++) {
                if (thumbList.get(i) == null) {
                    continue;
                }
                thumbMap.put(Long.valueOf(blogIdList.get(i).toString()), true);
            }
        }



        // 将博客列表转换为BlogVO列表，并设置是否点赞属性
    return list.stream().map(blog -> {
        BlogVO blogVO = new BlogVO();
        BeanUtil.copyProperties(blog,blogVO);
        blogVO.setHasThumb(thumbMap.get(blog.getId()));
        return blogVO;
    }).toList();
    }



    private BlogVO getBlogVoById(Blog blog,User loginUser){
        BlogVO blogVO = new BlogVO();
        BeanUtil.copyProperties(blog,blogVO);
        //如果用户没有登录 直接返回
        if (loginUser == null){
            return blogVO;
        }
        //如果用户登录，查看当前用户是否点赞当前文章
//        LambdaQueryWrapper<Thumb> queryWrapper = new LambdaQueryWrapper<>();
//       queryWrapper.eq(Thumb::getBlogId,blog.getId());
//       queryWrapper.eq(Thumb::getUserId,loginUser.getId());
//        Thumb one = thumbService.getOne(queryWrapper);
//        blogVO.setHasThumb(one != null);

        Boolean exist = thumbService.hasThumb(blog.getId(), loginUser.getId());
        blogVO.setHasThumb(exist);

        return blogVO;
    }
}




